package cn.marinda.gui.impl;

import cn.marinda.gui.AbstractHelper;
import cn.marinda.listener.*;
import cn.marinda.util.FileUtils;
import cn.marinda.util.HttpUtils;
import lombok.Data;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设置选择服务器核心文件实现界面
 *
 * @author marinda
 * @date 2022/08/02
 */
@Data
public class SettingSelectServerCoreFileHelperImpl extends AbstractHelper {

    /**
     * 创建服务端界面
     */
    public SettingSelectServerCoreFileHelperImpl(List<String> onlineList){
        super(onlineList);
    }


    protected JMenuBar loadMenuBar() {
//        一级菜单
        JMenu choice = new JMenu("选择");
        String[] choiceArray = new String[]{"服务端创建","在线核心","在线插件"};
        String[] serverArray = new String[]{"控制台","核心修改"};
        String[] settingArray = new String[]{"检查更新"};
        String[] aboutArray = new String[]{"关于作者","反馈内容"};
        JMenu server = new JMenu("服务器");
        JMenu setting = new JMenu("设置");
        JMenu aboutAuthor = new JMenu("帮助");
        Map<JMenu,List<JMenuItem>> menuListMap = new HashMap<>();
        List<JMenuItem> aboutItem = createJMenuItem(aboutArray);
        List<JMenuItem> choiceItem = createJMenuItem(choiceArray);
        List<JMenuItem> serverItem = createJMenuItem(serverArray);
        List<JMenuItem> settingItem = createJMenuItem(settingArray);
        choiceItem.get(choiceItem.size()-1).addActionListener(new OpenOnlinePluginEvent());
        choiceItem.get(choiceItem.size()-2).addActionListener(new OpenOnlineCoreEvent());
        serverItem.get(0).addActionListener(new ChangeServerConsoleActivityEvent());
        serverItem.get(1).addActionListener(new OpenSettingSelectServerCoreFileEvent());
//        禁用
        serverItem.get(1).setEnabled(false);
        choiceItem.get(0).addActionListener(new OpenCreateServerEvent());
        settingItem.get(0).addActionListener(new OpenCheckVersionEvent());
        aboutItem.get(0).addActionListener(new OpenAuthorInfoEvent());
        aboutItem.get(1).addActionListener(new OpenGroupInfoEvent());
        menuListMap.put(server,serverItem);
        menuListMap.put(setting,settingItem);
        menuListMap.put(choice,choiceItem);
        menuListMap.put(aboutAuthor,aboutItem);
        JMenuBar menuBar = createMenuBar(menuListMap);
        menuBar.add(aboutAuthor);
        return menuBar;
    }

    @Override
    protected JFrame loadFrame() { 
        frame = new JFrame();
        frame.setSize(700,500);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setTitle("选择核心");
        frame.setJMenuBar(menuBar);
        frame.setContentPane(view);
        frame.pack();
        return frame;
    }


    @Override
    protected JPanel loadPanel() {
        view = new JPanel();
        Box boxIndex1 = Box.createHorizontalBox();
        Box boxIndex2 = Box.createVerticalBox();
        Box boxIndex3 = Box.createVerticalBox();
        JLabel label = createTextComponent("当前核心列表");
        boxIndex1.add(Box.createHorizontalStrut(150));
        boxIndex1.add(label);
        boxIndex1.add(Box.createHorizontalGlue());
        boxIndex1.add(Box.createVerticalStrut(50));
        for(Component component : componentList){
            if(component instanceof JComboBox){
                JComboBox list = (JComboBox) component;
                boxIndex2.add(Box.createHorizontalStrut(500));
                boxIndex2.add(list);
                boxIndex2.add(Box.createGlue());
                continue;
            }
            if(component instanceof  JButton){
                JButton btn = (JButton) component;
                JComboBox<String> serverCore = (JComboBox<String>) getHelperComponent("ComboBoxUI");
                btn.addActionListener(new SettingDefaultServerCoreEvent(serverCore));
                btn.setForeground(Color.black);
                btn.setBackground(Color.RED);
                boxIndex3.add(Box.createVerticalStrut(50));
                boxIndex3.add(Box.createHorizontalStrut(300));
                boxIndex3.add(component);
                boxIndex3.add(Box.createVerticalStrut(20));
                continue;
            }
            boxIndex3.add(Box.createHorizontalStrut(100));
            boxIndex3.add(component);
        }
        boxPanel.add(boxIndex1);
        boxPanel.add(boxIndex2);
        boxPanel.add(boxIndex3);
        view.add(boxPanel);
        return view;
    }


    @Override
    protected List<Component> loadComponent() {
        JComboBox<String> list = createComboBoxComponent(onlineList.toArray());
        JButton downloadBtn = createButtonComponent("立即修改");
        JLabel tips = createTextComponent("修改启动核心项");
        tips.setForeground(Color.red);
        tips.setFont(new Font(null,Font.PLAIN,12));
        componentList.add(list);
        componentList.add(downloadBtn);
        componentList.add(tips);
        return componentList;
    }

}